﻿@using hc.epm.DataModel.Basic;
@using hc.Plat.Common.Extend;
@using hc.epm.Common;
@model IEnumerable<Base_Role>
@{
    ViewBag.Title = "角色管理";
    Layout = "~/Views/Shared/_LayoutHc.cshtml";
}
<div class="content-right date-wrap fl c">
    <div class="search mt5">
        <form class="layui-form" action="" method="get">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">角色名 : </label>
                    <div class="layui-input-inline"><input type="text" name="RoleName" id="RoleName" placeholder="请输入角色名" autocomplete="off" class="layui-input" value="@ViewBag.RoleName" /></div>
                    <input type="hidden" id="Belong" name="Belong" value="@ViewBag.Belong" />
                </div>
                <input type="submit" class="layui-btn layui-btn-radius layui-btn-warm" lay-filter="search" value="搜 索" />
            </div>
        </form>
    </div>
    <div class="main mt5">
        <div class="btns">
            <a href="javascript:;" data-module="@(ViewBag.Belong)Role" data-right="add" id="add" class="">新 增</a>
            <a href="javascript:;" data-module="@(ViewBag.Belong)Role" data-right="delete" id="delete" class="">删 除</a>
        </div>
        <div class="layui-form">
            <table class="layui-table">
                <thead>
                    <tr>
                        <th width="6%" style="text-align:center">
                            <input type="checkbox" lay-skin="primary" lay-filter="allChoose" />
                        </th>
                        <th width="6%" style="text-align:center">序号</th>
                        <th width="10%" style="text-align:center">角色名</th>
                        <th style="text-align:left">备注</th>
                        <th width="13%" style="text-align:center">设置权限</th>
                        <th width="10%" style="text-align:center">操作</th>
                    </tr>
                </thead>
                <tbody>
                    @{
                        int index = 0;
                    }
                    @if (Model != null && Model.Count() > 0)
                    {
                        foreach (var item in Model)
                        {
                            index++;
                            <tr>
                                <td style="text-align:center">
                                    @if (item.Belong == "Admin")
                                    {
                                        <input type="checkbox" lay-skin="primary" disabled="disabled" />
                                    }
                                    else
                                    {
                                        <input type="checkbox" lay-skin="primary" name="chName" />
                                    }
                                    <input type="hidden" name="id-@item.SId" id="id-@item.SId" value="@item.SId" />
                                </td>
                                <td class="table-center">@index</td>
                                <td class="table-center">@item.RoleName</td>
                                <td>@item.Remark</td>
                                <td class="table-center">
                                    <a href="javascript:;" class="text-link" data-belong="@(item.Belong)" data-itemid="@item.SId">设置功能权限</a>
                                </td>
                                <td class="table-center">
                                    @if (item.Belong == "Owner")
                                    {
                                        <a href="javascript:;" class="editor-link" data-itemid="@item.SId" title="编辑"><i class='glyphicon glyphicon-pencil handle-blue'></i></a>
                                    }
                                </td>
                            </tr>
                        }
                    }
                    else
                    {
                        <tr>
                            <td colspan="10" class="table-center">
                                暂无数据
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
        <div id="page"></div>
    </div>
</div>
<script>
    //若无数据隐藏分页组件
    if ('@ViewBag.Total' == 0) {
        $("#page").hide();
    }
    layui.use(['layer', 'form', 'element', 'laypage'], function () {
        var layer = layui.layer, form = layui.form, element = layui.element, $ = layui.jquery, laypage = layui.laypage;
        //全选
        form.on('checkbox(allChoose)', function (data) {
            var child = $(data.elem).parents('table').find('tbody input[name="chName"]');
            child.each(function (index, item) {
                item.checked = data.elem.checked;
            });
            form.render('checkbox');
        });
        //分页
        laypage.render({
            elem: 'page',
            count: @ViewBag.Total,
            curr:@ViewBag.pageIndex,
            layout: ['prev', 'page', 'next', 'skip','count', ],
            jump:function(obj,first){
                if (!first){
                    //得到了当前页，用于向服务端请求对应数据
                    var curr = obj.curr;
                    var search = $("form").serialize();
                    window.location.href = "/HCRole/Index?pageIndex=" + curr + "&" + search;
                }
            }
        });
        //修改
        $(".editor-link").click(function () {
            var id=$(this).data("itemid");
            if(id != ""){
                layer.open({
                    type: 2,
                    title:GetLayerTitle("@(ViewBag.BelongText)-编辑"),
                    shadeClose: false, //点击遮罩关闭层
                    area: [BigLayerWidth,'600px'],
                    content: '/HCRole/Edit/'+id
                });
            }

        });
        //增加
        $("#add").click(function () {
            layer.open({
                type: 2,
                title: GetLayerTitle("@(ViewBag.BelongText)-新增"),
                area: [BigLayerWidth,'600px'],
                content: '/HCRole/add?Belong=@ViewBag.Belong'
            });
        });
        //删除
        $("#delete").click(function () {
            var chkItems = $('.layui-table tbody input[type="checkbox"]:checked');
            var ids = "";
            chkItems.each(function (index, item) {
                var hfId = $(this).parent().find("input[type='hidden']");
                ids += hfId.val() + ",";
            });
            if (ids != "") {
                layer.confirm("确认要删除所选择的项？",{icon:3},function(){
                    $.post("Delete",{ids:ids,belong:'@ViewBag.Belong'},function(data){
                        if(data.Flag){
                            layer.msg("删除成功",{time:1000,icon:1});
                            var search = $("form").serialize();
                            window.location.href = "/HCRole/Index?pageIndex=1&" + search;
                        }else{
                            layer.alert(data.Message, { icon: 2 });
                        }
                    },"json");
                },function(){
                    return;
                })

            }else {
                layer.alert("请先选择要删除的数据", { icon: 2 });
            }
        });
        //设置权限
        $(".text-link").click(function () {
            var id = $(this).data("itemid");
            var belong = $(this).data("belong");
            var roleText="";
            if(belong=="Admin"){
                roleText="后台";
            }else if(belong=="Owner"){
                roleText="前台";
            }
            layer.open({
                type: 2,
                title: GetLayerTitle("设置"+roleText+"-功能权限"),
                shadeClose: false, //点击遮罩关闭层
                area: [BigLayerWidth, '600px'],
                content: '/HCRole/set?id='+id+'&Belong='+belong
            });
        })
    });
</script>
